package lambda_expression;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Stream使用
 */
public class App6 {


    public static void main(String[] args) {
        List<String> accounts = new ArrayList<String>(){{
            add("zhangsan");
            add("lisi");
            add("wangwu");
            add("wang");
            add("albinguo");
        }};

        // foreach形式
        for (String account: accounts) {
            if(account.length() > 5){
                System.out.println("有效账号:" + account);
            }
        }

        //迭代器形式
        Iterator<String> iterator = accounts.iterator();
        while(iterator.hasNext()){
           String account =iterator.next();
           if(account.length() > 5){
               System.out.println("有效账号:" + account);
           }
        }

        // Stream结合Lambda表达式，完成业务处理
        List<String> list = accounts.stream().filter(s -> s.length() > 5).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list,true ));
    }


}
